package com.example.abc.amqp;

import com.example.abc.util.JsonUtil;
import com.example.abc.web.IndexController;
import com.example.abc.websocket.msg.LogoutMsg;
import com.example.abc.websocket.msg.WsBaseMessage;
import com.example.abc.websocket.msg.WsMsgType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Peter on 2018/5/8.
 */
@Component
public class MqMsgSender {
    public static final Logger logger = LoggerFactory.getLogger(MqMsgSender.class);

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(WsBaseMessage msg) {
        String context = JsonUtil.toJson(msg);
        logger.info("MQ Sender : " + context);
        this.rabbitTemplate.convertAndSend("message", context);
    }

    public void send(LogoutMsg msg) {
        logger.info("MQ Sender LogoutMsg: " + msg.getTo());
        this.rabbitTemplate.convertAndSend(WsMsgType.logout.name(), msg);
    }
}
